<%@ page language="java" pageEncoding="UTF-8"%>
<script type="text/javascript">
	$(function() {

		$('#user-tree-menu${param.groupid}').menu({
			onClick : function(item) {
				var friendNode = $(this).data('friendNode');
				var clickType = $(item.target).attr("type");
				if ("remarkName" == clickType) {
					//修改备注名称
					$('<div/>').dialog({
						href : '${pageContext.request.contextPath}/app/user/talk/alter_remark.jsp',
						width : 300,
						height : 110,
						modal : true,
						title : '修改备注名称',
						buttons : [ {
							text : '修改',
							iconCls : 'icon-edit',
							handler : function() {
								//更新备注
								var alter_user_remarkName = $("#alter_user_remarkName").val();
								//销毁容器
								var d = $(this).closest('.window-body');
								d.dialog('destroy');
								updateFriendRemarkName(friendNode, '${param.groupid}', alter_user_remarkName);
							}
						} ],
						onLoad : function() {
							$("#alter_user_remarkName").val(friendNode.attributes.remarkName);
						},
						onClose : function() {
							$(this).dialog('destroy');
						}
					});
				}
				if ("deleteFriend" == clickType) {
					//删除好友
					var confirmMsg = '你确定要删除以下联系人吗？<p>&nbsp;&nbsp;<img src="${pageContext.request.contextPath}/images/cat32.png"><font color="red" size="3">' + friendNode.text + '</font></p>';
					$.messager.confirm('提示', confirmMsg, function(flag) {
						if (flag) {
							deleteFriend(friendNode, "${param.groupid}");
						}
					});
				}
			}
		});

		$("#user-friend-tree${param.groupid}").tree({
			url : '${pageContext.request.contextPath}/friendAction!getFriend.action?groupid=${param.groupid}',
			lines : false,
			onContextMenu : function(e, node) {
				e.preventDefault();
				//添加可移动的组到菜单
				var itemMenu = $('#user-tree-menu${param.groupid}');
				// 移除菜单中的移动联系人至，否则每次组都会叠加
				itemMenu.menu('removeItem', $("#friend-tree-moveGroup${param.groupid}")[0]);
				// 把移动联系人至附加到菜单中
				itemMenu.menu('appendItem', {
					id : 'friend-tree-moveGroup${param.groupid}',
					text : '移动联系人至'
				});
				var item = itemMenu.menu('getItem', $("#friend-tree-moveGroup${param.groupid}")[0]);
				if (groups && groups.length > 0) {
					for ( var x = 0; x < groups.length; x++) {
						var group = groups[x];
						//非陌生人组和当前组
						if ('1' != group.id && '${param.groupid}' != group.id) {
							itemMenu.menu('appendItem', {
								parent : item.target,
								text : group.groupname,
								onclick : function() {
									alert(1231);
								}
							});
						}
					}
				}

				//人员闪烁时，不可以用菜单
				if (!node.attributes.intervalId || isNaN(node.attributes.intervalId)) {
					$('#user-tree-menu${param.groupid}').menu('show', {
						left : e.pageX,
						top : e.pageY
					}).data('friendNode', node);
				}
			},
			onDblClick : function(node) {
				if (node.id != "${userSession.id}") {
					//打开对话框
					addUserDialogTab(node.id, node.text);

					//闪烁线程id不为空并且为数字，则取消闪烁并加载消息
					if (node.attributes.intervalId && !isNaN(node.attributes.intervalId)) {
						//人员通知线程id map
						var ump = notifyMap.get(node.attributes.group.id);
						for ( var i = 0; i < ump.values()[0].length; i++) {
							if (ump.values()[0][i] == node.attributes.intervalId) {
								ump.values()[0].splice(i, 1);
							}
						}

						//组中没有人员消息通知，取消组名称闪烁
						if (ump.values()[0].length < 1) {
							window.clearInterval(ump.keys()[0]);
							$('#talk-contacts-frame').accordion("getPanel", node.attributes.groupIndex).panel("setTitle", node.attributes.group.groupname);
						}
						//取消闪烁
						window.clearInterval(node.attributes.intervalId);
						$(this).tree('update', {
							target : node.target,
							text : node.attributes.sourceName
						});
						//清空人员闪烁线程id
						node.attributes.intervalId = null;
						//加载消息
						var loadingmsgPre = setWaitMsg("#messageView" + node.id);
						var messageViewDiv = $("#messageView" + node.id);
						$.ajax({
							type : "post",
							dataType : "json",
							url : "${pageContext.request.contextPath}/talkContextAction!getContext.action",
							data : "sendUserid=" + node.id,
							error : function(info) {
								eval(info.responseText);
							},
							success : function(msgs) {
								var msg = "";
								if (msgs && msgs.length > 0) {
									for ( var q = 0; q < msgs.length; q++) {
										var msgJson = msgs[q];
										msg += "<font color='blue'>" + msgJson.sendUsername + "  " + msgJson.sendTime + "</font><br><font style='font-size: 14px'>&nbsp;&nbsp;" + msgJson.msg + "</font><br>";
									}
								}
								//停止放加载图片js
								window.clearInterval(loadingmsgPre);
								//把信息放到显示框中
								if (!messageViewDiv[0]) {
									setMsgToViewDiv("#messageView" + node.id, msg);
								} else {
									//判断是否有加载消息提示，有则去掉
									if ($("#loadmsg" + loadingmsgPre)[0]) {
										$("#loadmsg" + loadingmsgPre).html(msg);
									} else {
										messageViewDiv.append(msg);
									}
									//让div滚动条保持在最下面
									var scrollTop = messageViewDiv[0].scrollHeight;
									messageViewDiv.scrollTop(scrollTop);
								}
							}
						});
					}
				} else {
					$.messager.alert('提示', '不能与自己聊天！');
				}
			}
		});
	});
</script>
<ul id="user-friend-tree${param.groupid}" style="margin-top: 5px;"></ul>
<div id="user-tree-menu${param.groupid}" style="width: 120px;display:none;">
	<div id="friend-tree-moveGroup${param.groupid}" type="moveGroup">移动联系人至</div>
	<div id="friend-tree-remarkName${param.groupid}" type="remarkName">修改备注名称</div>
	<div id="friend-tree-deleteFriend${param.groupid}" type="deleteFriend">删除好友</div>
</div>